<template>
  <div class="dialog-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆" prop="vehicleId">
        <el-select clearable filterable v-model="queryParams.vehicleId" @keyup.enter="handleQuery" placeholder="请选择主线路">
          <el-option v-for="item in showVehicleList" :key="item.numbered" :label="item.plateNo+'('+item.numbered+')'"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" :disabled="multiple" @click="handleAdd"
          v-hasPermi="['basic:routeVehicle:add']">添加已选车辆</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table max-height="500" v-loading="loading" :data="routeVehicleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车牌号" align="center" prop="plateNo" />
      <el-table-column label="车号" align="center" prop="numbered" />
      <el-table-column label="车牌颜色" align="center" prop="color" >
        <template #default="scope">
          <dict-tag :options="basic_vehicle_color" :value="scope.row.color" />
        </template>
        </el-table-column>
      <el-table-column label="车辆类型" align="center" prop="type" >
        <template #default="scope">
          <dict-tag :options="basic_vehicle_type" :value="scope.row.type" />
        </template>
        </el-table-column>
      <el-table-column label="车辆运营状态" align="center" prop="outageStatus" >
        <template #default="scope">
          <dict-tag :options="basic_vehicle_outage_status" :value="scope.row.outageStatus" />
        </template>
        </el-table-column>
      <el-table-column label="车辆状态 " align="center" prop="status" >
        <template #default="scope">
          <dict-tag :options="sys_data_status" :value="scope.row.status" />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup name="AddVehicleList">
import { queryAddVehicleList,addRouteVehicleList } from "@/api/basic/routeVehicle";
import { ref } from "vue";
const { proxy } = getCurrentInstance();

const { basic_vehicle_color, basic_vehicle_type, basic_vehicle_outage_status, sys_data_status } = proxy.useDict('basic_vehicle_color', 'basic_vehicle_type', 'basic_vehicle_outage_status', 'sys_data_status');
const props = defineProps({
  // 当前的值
  routeId: [Number, String]
});
const showVehicleList = ref([]);
const routeVehicleList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    routeId: undefined,
    vehicleId: undefined
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询线路车辆列表 */
function getList() {
  loading.value = true;
  queryAddVehicleList(queryParams.value).then(response => {
    if(showVehicleList.value.length == 0){
      showVehicleList.value = response.data
    }
    routeVehicleList.value = response.data; 
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    routeId: props.routeId,
    vehicleId: null,
    plateNo: null,
    numbered: null,
    color: null,
    type: null,
    netState: null,
    operationRoute: null,
    transportType: null,
    outageStatus: null,
    status: null,
    deptId: props.deptId,
  };
  proxy.resetForm("routeVehicleRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
const emit = defineEmits(['opEnd'])
/** 批量新增按钮操作 */
function handleAdd() {
  proxy.$modal.confirm('是否添加"' + ids.value.length + '"辆车？"').then(function () {
    loading.value = true;
    let addParams = {
      routeId:props.routeId,
      vehicleIds:ids.value
    }
    return addRouteVehicleList(addParams);
  }).then((res) => {
    loading.value = true;
    proxy.$modal.msgSuccess("添加成功");
    emit('opEnd',{status:'success'})
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}
watch(props, (newVal, oldVal) => {
  queryParams.value.routeId = newVal.routeId
  getList()
}, { immediate: true });
</script>
